/*
 *	Yalala
 *	
 *	Copyright © MediaMonks B.V.
 *	All rights reserved.
 *	
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions are met:
 *	1. Redistributions of source code must retain the above copyright
 *	   notice, this list of conditions and the following disclaimer.
 *	2. Redistributions in binary form must reproduce the above copyright
 *	   notice, this list of conditions and the following disclaimer in the
 *	   documentation and/or other materials provided with the distribution.
 *	3. All advertising materials mentioning features or use of this software
 *	   must display the following acknowledgement:
 *	   This product includes software developed by MediaMonks B.V.
 *	4. Neither the name of MediaMonks B.V. nor the
 *	   names of its contributors may be used to endorse or promote products
 *	   derived from this software without specific prior written permission.
 *	
 *	THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
 *	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *	DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
 *	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	
 */

package com.mediamonks.yalala.data.vo 
{
	import nl.acidcats.yalog.common.Levels;

	import temple.core.CoreObject;
	import temple.data.collections.HashMap;
	import temple.utils.types.ArrayUtils;

	/**
	 * @author Thijs Broerse
	 */
	public class SettingsData extends CoreObject 
	{
		private static const _NUM_LEVELS:uint = 6;
		
		public static const TEXT:String = 'text';
		public static const SENDER:String = 'sender';
		public static const SENDERID:String = 'senderId';
		public static const STACKTRACE:String = 'stackTrace';
		
		public static const AUTO_SCROLL:String = 'autoScroll';
		
		private var _levels:Vector.<Boolean>;
		private var _autoScroll:Boolean;
		
		private var _filterPresets:HashMap;
		
		public function SettingsData()
		{
			this._levels = new Vector.<Boolean>(_NUM_LEVELS, true);
			
			this._filterPresets = new HashMap("Filters");
			this._filterPresets[TEXT] = [];
			this._filterPresets[SENDER] = [];
			this._filterPresets[SENDERID] = [];
			this._filterPresets[STACKTRACE] = [];
		}

		public function reset():void 
		{
			// default values
			this._levels[Levels.DEBUG] = true;
			this._levels[Levels.INFO] = true;
			this._levels[Levels.ERROR] = true;
			this._levels[Levels.WARN] = true;
			this._levels[Levels.FATAL] = true;
			this._levels[Levels.STATUS] = false;
			this._autoScroll = true;
		}
		
		public function get levels():Vector.<Boolean>
		{
			return this._levels;
		}
		
		/**
		 * Use * as accessor since Flash has problems to store a Vector.<Boolean> in a SharedObject
		 */
		public function set levels(value:*):void
		{
			var leni:int = Math.min(value.length, _NUM_LEVELS);
			for (var i:int = 0; i < leni; i++)
			{
				this._levels[i] = value[i] as Boolean;
			}
		}
		
		public function get autoScroll():Boolean
		{
			return this._autoScroll;
		}
		
		public function set autoScroll(value:Boolean):void
		{
			this._autoScroll = value;
		}
		
		public function getFilterPreset(field:String):Array
		{
			return this._filterPresets[field] as Array;
		}

		public function addFilterPreset(field:String, filter:String):void
		{
			var presets:Array = this._filterPresets[field] as Array;
			if (presets.indexOf(filter) == -1) presets.push(filter);
		}
		
		public function removeFilterPreset(field:String, filter:String):void
		{
			ArrayUtils.removeValueFromArray(this._filterPresets[field], filter);
		}
		
		public function get filterPresets():HashMap
		{
			return this._filterPresets;
		}
		
		public function set filterPresets(value:*):void
		{
			for (var key : String in this._filterPresets)
			{
				if (value[key] is Array)
				{
					this._filterPresets[key] = value[key];
				}
			}
		}
	}
}
